package com.wwwarehouse.common.tools;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

/**
 * Created by jack.dong on 2017/1/10.
 */

public class AnimationUtils {
    public interface OnAnimationFinishListener {
        public void onFinish();
    }

    static OnAnimationFinishListener mOnAnimationFinishListener = null;
    public static void setOnAnimationFinishListener(OnAnimationFinishListener listener) {
        mOnAnimationFinishListener = listener;
    }

    /*-----------------------------------------------------------------------------------------------*/
    /**
     * 水平翻转
     * @param view cardView
     * @param mLlFront 前面的整个布局
     * @param mRlBack 背面的整个布局
     * @param handler 需要监听动画结束传
     * @return 动画集合
     */
    private static final int duration = 500;
    public static AnimatorSet cardTransAnims(View view, final View mLlFront, final View mRlBack, final Handler handler) {
        int direction = 1;
        if (mLlFront.isShown()) {
            direction = -1;
        }
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mRlBack.isShown()) {
                    mRlBack.setVisibility(View.GONE);
                    mLlFront.setVisibility(View.VISIBLE);
                } else {
                    mRlBack.setVisibility(View.VISIBLE);
                    mLlFront.setVisibility(View.GONE);
                    if (handler != null && mOnAnimationFinishListener != null) {
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mOnAnimationFinishListener.onFinish();
                            }
                        }, 1000);
                    }
                }

            }
        }, duration);

        if (direction != 1 && direction != -1) direction = 1;
        view.setCameraDistance(16000*view.getResources().getDisplayMetrics().density);
        AnimatorSet animSet = new AnimatorSet();
        ObjectAnimator rotationY = new ObjectAnimator();
        rotationY.setDuration(duration).setPropertyName("rotationY");
        rotationY.setFloatValues(0, -90*direction);
        ObjectAnimator _rotationY = new ObjectAnimator();
        _rotationY.setDuration(duration).setPropertyName("rotationY");
        _rotationY.setFloatValues(90*direction, 0);
        _rotationY.setStartDelay(duration);
        ObjectAnimator scale = new ObjectAnimator();
        scale.setDuration(duration).setPropertyName("scaleY");
        scale.setFloatValues(1, 0.94f);
        ObjectAnimator _scale = new ObjectAnimator();
        _scale.setDuration(duration).setPropertyName("scaleY");
        _scale.setFloatValues(0.94f, 1);
        _scale.setStartDelay(duration);
        animSet.setTarget(view);
        rotationY.setTarget(view);
        _rotationY.setTarget(view);
        scale.setTarget(view);
        _scale.setTarget(view);
        animSet.playTogether(rotationY, _rotationY, scale, _scale);
        animSet.start();
        return animSet;
    }


	//	异常提示
	public static void showExceptionPrompt(String text, TextView textView) {
		textView.setText(text);
		float translationY = textView.getTranslationY();
		int height = textView.getHeight();
		ObjectAnimator animatorMoveIn = ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY, translationY + height + 10);
		animatorMoveIn.setDuration(500);
		ObjectAnimator animatorMoveOut = ObjectAnimator.ofFloat(textView, View.TRANSLATION_Y, translationY + height + 10, translationY);
		animatorMoveOut.setDuration(500);
		animatorMoveOut.setStartDelay(1000);
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.play(animatorMoveIn).before(animatorMoveOut);
		animatorSet.start();
	}
}
